<template>
  <div id="Login">
    <el-form ref="loginF" :model="loginForm" id="loginForm" :rules="rules" >
      <el-form-item class="padding-inline-20px" prop="oauthId">
        <el-input
          placeholder="请输入用户名/手机号/Email"
          prefix-icon="el-icon-user"
          v-model="loginForm.oauthId"
          clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item class="padding-inline-20px margin-bottom-5" prop="password">
        <el-input
          placeholder="请输入密码"
          prefix-icon="el-icon-lock"
          v-model="loginForm.password"
          show-password
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item class="margin-bottom-0 padding-inline-20px">
        <el-checkbox v-model="checked" @change="!checked">下次自动登录</el-checkbox>
      </el-form-item>

      <el-form-item class="margin-bottom-10px">
        <el-button
          type= "primary"
          @click= "submitForm()" 
          style= "width: 260px"
          >登录</el-button
        >
      </el-form-item>
      <el-form-item class="margin-bottom-0">
        <div id="orders">
          <router-link to="/register">注册</router-link>
          <router-link to="/forget">忘记密码?</router-link>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import api from "../api/api.js";
import { ref, reactive, toRefs } from 'vue'
import axios from '@/utils/axios'
import { useRouter } from 'vue-router'
import md5 from "js-md5"
export default {
  name: 'Login',
  setup() {
    const loginF = ref(null)
    const router = useRouter()
    const state = reactive({
      loginForm: {
        oauthId: "",
        password: "",
        authType: 0,
      },
      checked: false,
      rules: {
        oauthId: [
          { required: true, message: "尚未输入账号", trigger: "blur" },
          { min: 3, message: "长度至少 3 位以上", trigger: "blur" },
        ],
        password: [
          { required: true, message: "尚未输入密码", trigger: "blur" },
          {
            min: 6,
            max: 18,
            message: "长度在 6 到 18 个字符",
            trigger: "blur",
          },
        ],
      }
    })
    const submitForm = () => {
      loginF.value.validate((valid) => {
        if (valid) {
          // if (/^1[3-9]\d{9}$/.test(state.loginForm.oauthId)) {
          //   state.loginForm.authType = 1;
          // } else if (
          //   state.loginForm.oauthId.search(
          //     /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
          //   ) != -1
          // ) {
          //   state.loginForm.authType = 2;
          // } else {
          //   state.loginForm.authType = 0;
          // }
          console.log(state.loginForm);

          axios.post(api.account.login + "?username=" + state.loginForm.oauthId + "&password=" + md5(state.loginForm.password)).then((res) => {
            console.log(res);
            if (res.data.code == "00000") {
              let user = res.data.data;
              console.log(user);
              router.push({ path: '/' })
            } else {
              alert(res.data.message);
            }
          });
        } else {
          alert("账号或者密码格式错误，请重新输入");
          return false;
        }
      });
    }
    return {
      ...toRefs(state),
      loginF,
      submitForm
    }
  }
}
</script>

<style>
#Login {
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: Center;
  height: 100%;
}
#loginForm {
  padding-top: 40px;
  width: 300px;
  border: 1px solid #ddd;
  height: 250px;
  border-radius: 2px;
}
#orders {
  display: flex;
  justify-content: space-around;
}
.margin-bottom-0 {
  margin-bottom: 0;
}
.margin-bottom-5 {
  margin-bottom: 5px;
}
.margin-bottom-10px {
  margin-bottom: 10px;
}
.padding-inline-20px {
  padding-inline: 20px;
}
</style>